package com.nf.health.app.activity.mesure_plan;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.datamain.activity.BloodPressureActivity;
import com.nf.health.app.datamain.activity.BloodSugarDataActivity;
import com.nf.health.app.bodyfat.BodyFatDataNewActivity;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.intervention_details.adapter.DetectionAdapter;
import com.nf.health.app.intervention_details.bean.DetectionVo;
import com.nf.health.app.models.DeviceData;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DateUtil;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 测量计划  详情页
 * Created by Ian on 2017/5/27.
 */
public class MesureDetailsActivity extends BaseActivity {
    DetectionVo detectionVo = new DetectionVo();
    @Bind(R.id.detection_listview_xueya)
    MyListView detectionListview;
    @Bind(R.id.content_xueya)
    TextView content;
    @Bind(R.id.wenxintishi)
    TextView wenxintishi;
    @Bind(R.id.yizhouanpai)
    TextView yizhouanpai;
    @Bind(R.id.jiance_xueya)
    Button jiance;
    @Bind(R.id.detection_listview_xuetang)
    MyListView detectionListviewXuetang;
    @Bind(R.id.content_xuetang)
    TextView contentXuetang;
    @Bind(R.id.jiance_xuetang)
    Button jianceXuetang;
    @Bind(R.id.detection_listview_tizhi)
    MyListView detectionListviewTizhi;
    @Bind(R.id.content_tizhi)
    TextView contentTizhi;
    @Bind(R.id.jiance_tizhi)
    Button jianceTizhi;
    @Bind(R.id.lin1)
    LinearLayout lin1;
    @Bind(R.id.lin2)
    LinearLayout lin2;
    @Bind(R.id.lin3)
    LinearLayout lin3;
    //血压时间
    @Bind(R.id.xueya_liudianban)
    LinearLayout xueyaLiudianban;
    @Bind(R.id.xueya_jiudianban)
    LinearLayout xueyaJiudianban;
    @Bind(R.id.xueya_shidian)
    LinearLayout xueyaShidian;
    @Bind(R.id.xueya_shisandianban)
    LinearLayout xueyaShisandianban;
    @Bind(R.id.xueya_shisidianban)
    LinearLayout xueyaShisidianban;
    @Bind(R.id.xueya_ershidian)
    LinearLayout xueyaErshidian;
    @Bind(R.id.xueya_ershierdian)
    LinearLayout xueyaErshierdian;
    //血糖时间
    @Bind(R.id.xuetang_liudianban)
    LinearLayout xuetangLiudianban;
    @Bind(R.id.xuetang_jiudianban)
    LinearLayout xuetangJiudianban;
    @Bind(R.id.xuetang_shidian)
    LinearLayout xuetangShidian;
    @Bind(R.id.xuetang_shisandianban)
    LinearLayout xuetangShisandianban;
    @Bind(R.id.xuetang_shisidianban)
    LinearLayout xuetangShisidianban;
    @Bind(R.id.xuetang_ershidian)
    LinearLayout xuetangErshidian;
    @Bind(R.id.xuetang_ershierdian)
    LinearLayout xuetangErshierdian;
    //体质时间
    @Bind(R.id.tizhi_liudianban)
    LinearLayout tizhiLiudianban;
    @Bind(R.id.tizhi_jiudianban)
    LinearLayout tizhiJiudianban;
    @Bind(R.id.tizhi_shidian)
    LinearLayout tizhiShidian;
    @Bind(R.id.tizhi_shisandianban)
    LinearLayout tizhiShisandianban;
    @Bind(R.id.tizhi_shisidianban)
    LinearLayout tizhiShisidianban;
    @Bind(R.id.tizhi_ershidian)
    LinearLayout tizhiErshidian;
    @Bind(R.id.tizhi_ershierdian)
    LinearLayout tizhiErshierdian;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.tv_suggest)
    TextView tv_suggest;
    @Bind(R.id.btn_mesure)
    Button btnMesure;

    String style;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitleBar(R.layout.titlebar_base);//  title 栏
        setContent(R.layout.activity_mesure_details);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titlebarTitleTv.setText("测量计划");
        String uid = getIntent().getStringExtra("uid");
        provider.requesprmonitor(uid, "detection");

        jiance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("deviceType", DeviceData.Type.BLOOD_PRESSURE);
                ActivityUtils.showActivity(MesureDetailsActivity.this, BloodPressureActivity.class, bundle);
            }
        });
        jianceXuetang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("deviceType", DeviceData.Type.BLOOD_SUGAR);
                ActivityUtils.showActivity(MesureDetailsActivity.this, BloodSugarDataActivity.class, bundle);
            }
        });
        jianceTizhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("deviceType", DeviceData.Type.BLOOD_PRESSURE);
                bundle.putString("deviceType", DeviceData.Type.BALANCE);
                ActivityUtils.showActivity(MesureDetailsActivity.this, BodyFatDataNewActivity.class, bundle);
            }
        });


        btnMesure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(style)){
                    return;
                }else{
                    if("blood".equals(style)){
                        Bundle bundle = new Bundle();
                        bundle.putString("deviceType", DeviceData.Type.BLOOD_PRESSURE);
                        ActivityUtils.showActivity(MesureDetailsActivity.this, BloodPressureActivity.class, bundle);
                    }else if("balance".equals(style)){
                        Bundle bundle = new Bundle();
                        bundle.putString("deviceType", DeviceData.Type.BLOOD_PRESSURE);
                        bundle.putString("deviceType", DeviceData.Type.BALANCE);
                        ActivityUtils.showActivity(MesureDetailsActivity.this, BodyFatDataNewActivity.class, bundle);
                    }else if("bloodsugar".equals(style)){
                        Bundle bundle = new Bundle();
                        bundle.putString("deviceType", DeviceData.Type.BLOOD_SUGAR);
                        ActivityUtils.showActivity(MesureDetailsActivity.this, BloodSugarDataActivity.class, bundle);
                    }
                }
            }
        });


    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if ("detection".equals(actionName)) {
            detectionVo = (DetectionVo) object;

            if (detectionVo.getBlood().size() > 0) {//判断布局是否隐藏
                lin1.setVisibility(View.VISIBLE);
                btnMesure.setVisibility(View.GONE);
                //血压判断
                if (detectionVo.getBlood().get(0).getTime0630() == null) {
                    xueyaLiudianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBlood().get(0).getTime0930() == null) {
                    xueyaJiudianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBlood().get(0).getTime1000() == null) {
                    xueyaShidian.setVisibility(View.GONE);
                }
                if (detectionVo.getBlood().get(0).getTime1330() == null) {
                    xueyaShisandianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBlood().get(0).getTime1430() == null) {
                    xueyaShisidianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBlood().get(0).getTime2000() == null) {
                    xueyaErshidian.setVisibility(View.GONE);
                }
                if (detectionVo.getBlood().get(0).getTime2200() == null) {
                    xueyaErshierdian.setVisibility(View.GONE);
                }
                DetectionAdapter detectionAdapter = new DetectionAdapter(this, detectionVo.getBlood());//血压列表
                detectionListview.setAdapter(detectionAdapter);
            } else {
                lin1.setVisibility(View.GONE);
            }
            if (detectionVo.getBloodsugar().size() > 0) {
                lin2.setVisibility(View.VISIBLE);
                btnMesure.setVisibility(View.GONE);
                //血糖判断
                if (detectionVo.getBloodsugar().get(0).getTime0630() == null) {
                    xuetangLiudianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBloodsugar().get(0).getTime0930() == null) {
                    xuetangJiudianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBloodsugar().get(0).getTime1000() == null) {
                    xuetangShidian.setVisibility(View.GONE);
                }
                if (detectionVo.getBloodsugar().get(0).getTime1330() == null) {
                    xuetangShisandianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBloodsugar().get(0).getTime1430() == null) {
                    xuetangShisidianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBloodsugar().get(0).getTime2000() == null) {
                    xuetangErshidian.setVisibility(View.GONE);
                }
                if (detectionVo.getBloodsugar().get(0).getTime2200() == null) {
                    xuetangErshierdian.setVisibility(View.GONE);
                }
                DetectionAdapter detectionAdapter1 = new DetectionAdapter(this, detectionVo.getBloodsugar());//血糖列表
                detectionListviewXuetang.setAdapter(detectionAdapter1);

            } else {
                lin2.setVisibility(View.GONE);
            }
            if (detectionVo.getBalance().size() > 0) {
                lin3.setVisibility(View.VISIBLE);
                btnMesure.setVisibility(View.GONE);
                //体质判断
                if (detectionVo.getBalance().get(0).getTime0630() == null) {
                    tizhiLiudianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBalance().get(0).getTime0930() == null) {
                    tizhiJiudianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBalance().get(0).getTime1000() == null) {
                    tizhiShidian.setVisibility(View.GONE);
                }
                if (detectionVo.getBalance().get(0).getTime1330() == null) {
                    tizhiShisandianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBalance().get(0).getTime1430() == null) {
                    tizhiShisidianban.setVisibility(View.GONE);
                }
                if (detectionVo.getBalance().get(0).getTime2000() == null) {
                    tizhiErshidian.setVisibility(View.GONE);
                }
                if (detectionVo.getBalance().get(0).getTime2200() == null) {
                    tizhiErshierdian.setVisibility(View.GONE);
                }
                DetectionAdapter detectionAdapter2 = new DetectionAdapter(this, detectionVo.getBalance());//体质列表
                detectionListviewTizhi.setAdapter(detectionAdapter2);
            } else {
                lin3.setVisibility(View.GONE);
            }
            content.setText(detectionVo.getGuide().getBlood());//血压详情
            contentXuetang.setText(detectionVo.getGuide().getBloodsugar());//血糖
            contentTizhi.setText(detectionVo.getGuide().getBalance());//体质
            yizhouanpai.setText("一周监测安排" + "(" + DateUtil.dateToString(new Date(Long.parseLong(detectionVo.getStarttime())), "yyyy-MM-dd") + "至" + DateUtil.dateToString(new Date(Long.parseLong(detectionVo.getEndtime())), "yyyy-MM-dd") + ")");

            if (!TextUtils.isEmpty(detectionVo.getBloodtime())) {//高血压高位
                wenxintishi.setText(detectionVo.getSuggest());
                tv_suggest.setText(Html.fromHtml(detectionVo.getBloodtime()));
                btnMesure.setVisibility(View.VISIBLE);
                style = "blood";
            } else if (!TextUtils.isEmpty(detectionVo.getBalancetime())) {
                wenxintishi.setText(detectionVo.getSuggest());
                tv_suggest.setText(Html.fromHtml(detectionVo.getBalancetime()));
                btnMesure.setVisibility(View.VISIBLE);
                style = "balance";
            } else if (!TextUtils.isEmpty(detectionVo.getBloodsugartime())) {//糖尿病高位
                wenxintishi.setText(detectionVo.getSuggest());
                tv_suggest.setText(Html.fromHtml(detectionVo.getBloodsugartime()));
                btnMesure.setVisibility(View.VISIBLE);
                style = "bloodsugar";
            } else {
                wenxintishi.setText(detectionVo.getSuggest());

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}